<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ------------------ Danh sach quan huyen---------------------*/
class Districts extends Admin_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->library('Form_validation');
		$this->load->model('Districts_model');
		$this->load->helper('url');
		$this->lang->load('admin/user');
		$this->lang->load('admin/common');
	}
	
	public function getTypeList()
	{
		/*------------------------------------------------------------------------------*/
		$where='';
		$sidx='id';
		$sord='asc';
		$start='0';
		$limit='1000';
		$select = "id, dis_name, province_id, latx,lngx,laty,lngy";
		$result = $this->Districts_model->fetch($select, $where, $sidx, $sord, $start, $limit);
		$i=0;
		foreach($result as $userArray)
		{
			$responce->rows[$i]['id']=$userArray->id;
			$responce->rows[$i]['name']=array($userArray->id,$userArray->dis_name,$userArray->province_id,$userArray->latx,$userArray->lngx,$userArray->laty,$userArray->lngy,$userArray->province_id);
			$i++;
		}
		echo json_encode($responce);
	}
	/*---------------------Bat dau Xem tat ca cac ban ghi--------------------------*/
    public function getAll()
	{
		/*------------------------------------------------------------------------------*/
		if(isset($_POST['page']))
			$page =$_POST['page'] ; // get the requested page
		else $page=1;
		if(isset($_POST['rows']))
			$limit=$_POST['rows'];// get how many rows we want to have into the grid
		else $limit = 1;
		if(isset($_POST['sidx']))
			$sidx=$_POST['sidx'];// get index row - i.e. user click to sort
		else $sidx = 'id';
		if(!$sidx) $sidx ='id';
		if(isset($_POST['sord']))
			$sord=$_POST['sord'];// get the direction
		else $sord= 'asc';
		$total_pages=0;
		$where='';
		$by=$sord;
		$order=$sidx;
		//echo 'sap xep:'.$sidx;
		//echo 'thu thu:'.$sord;
		
		$count = count($this->Districts_model->fetch("id", $where, "", ""));
		if( $count >0 ) {
			$total_pages = ceil($count/$limit);
		} else { $total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		
		$select = "id, dis_name, province_id, latx,lngx,laty,lngy";
		$result = $this->Districts_model->fetch($select, $where, $sidx, $sord, $start, $limit);
		 
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		$i=0;
		foreach($result as $userArray)
		{
			$responce->rows[$i]['id']=$userArray->id;
			$responce->rows[$i]['cell']=array($userArray->id,$userArray->dis_name,$userArray->province_id,$userArray->latx,$userArray->lngx,$userArray->laty,$userArray->lngy,$userArray->province_id);
			$i++;
		}
		echo json_encode($responce);
	}
	/*---------------------Ket thuc Xem tat ca cac ban ghi--------------------------*/
	
	private function add()
	{
		$dataAdd = array(
				'dis_name'      =>     	trim(($this->filter->injection_html($this->input->post('name_edit')))),
				'province_id'   =>    	trim(($this->filter->injection_html($this->input->post('city_edit')))),
				'latx'       	=>      trim(($this->filter->injection_html($this->input->post('latx_edit')))),
		        'lngy'          =>      trim(($this->filter->injection_html($this->input->post('lngx_edit')))),
				'laty'          =>      trim(($this->filter->injection_html($this->input->post('laty_edit')))),
				'lngy'          =>      trim(($this->filter->injection_html($this->input->post('lngy_edit'))))
				 
		);
		 if($this->Districts_model->add($dataAdd))
		 {
			$responce->status=session_id();
			$responce->ret='success';
		 	echo json_encode($responce);
		 }else
		 {
		 	$responce->status=session_id();
			$responce->ret='failure';
		 	echo json_encode($responce);
		 }
		   
	}
   private function edit()
	{
		
		$id=(int)($this->filter->injection_html($this->input->post('id_edit')));
		$dataEdit = array(
					'dis_name'  	=>     	trim(($this->filter->injection_html($this->input->post('name_edit')))),
					'province_id'   =>    	trim(($this->filter->injection_html($this->input->post('city_edit')))),
					'latx'       	=>      trim(($this->filter->injection_html($this->input->post('latx_edit')))),
			        'lngy'          =>      trim(($this->filter->injection_html($this->input->post('lngx_edit')))),
					'laty'          =>      trim(($this->filter->injection_html($this->input->post('laty_edit')))),
					'lngy'          =>      trim(($this->filter->injection_html($this->input->post('lngy_edit'))))
				 
		);
		  
		
		if($this->Districts_model->update($dataEdit, "id = ".(int)$id))
		{
			$responce->status=session_id();
			$responce->ret='success';
		 	echo json_encode($responce);
		 }else
		 {
		 	$responce->status=session_id();
			$responce->ret='failure';
		 	echo json_encode($responce);
		 }
			
	}
   private function del()
	{
		$id=(int)($this->filter->injection_html($this->input->post('id_edit')));
		if($this->Districts_model->delete($id, "id"))
		{
			$responce->status=session_id();
			$responce->ret='success';
		 	echo json_encode($responce);
		}else
		{
			$responce->status=session_id();
			$responce->ret='failure';
		 	echo json_encode($responce);
		}
		
	}
    private function active()
	{
		echo $_POST['userName'];
	}
	private function deActive()
	{
		echo $_POST['userName'];
	}
	/*-----------------------Bat dau xu ly thong tin: them sua xoa...-------------*/
	public function process()
	{
		$act=$this->filter->injection_html($this->input->post('act'));
		switch ($act) 
		{
		    case "add"://add
		        $this->add();
		        break;
		    case "edit"://edit
		        $this->edit();
		        break;
		    case "del"://delete
		        $this->del();
		        break;
		     case "active"://kich hoat
		        $this->active();
		        break;
		     case "deActive":
		        $this->deActive();
		        break;
		     default:
		     	echo 'No process';
		}
	}
	
	
	
public function index()
	{
		$this->load->view('admin/district/view_districts');
	}
}
